<template>
	<div class="flowable-modeler-container">
		<el-card shadow="hover" class="mb10" v-auth="'flowable:modeler:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.modelName" placeholder="请输入模板名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="search.modelKey" placeholder="请输入流程标识" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="search.category" placeholder="请输入流程类别" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-table ref="tableModelerDeployRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="缩略图" prop="diagramUrl" width="120">
					<template #default="scope">
						<el-image style="width: 90px; height: 30px;"
							:src="scope.row.diagramUrl"
							:preview-teleported="true"
							:preview-src-list="[scope.row.diagramUrl]">
						</el-image>
					</template>
				</el-table-column>
				<el-table-column label="流程ID"  prop="deployId" show-overflow-tooltip></el-table-column>
				<el-table-column label="流程名称" prop="modelName" show-overflow-tooltip></el-table-column>
				<el-table-column label="流程标识" prop="modelKey" show-overflow-tooltip></el-table-column>
				<el-table-column label="流程类别" prop="category" show-overflow-tooltip></el-table-column>
				<el-table-column label="部署时间" prop="deployTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="125" v-auths="['flowable:modeler:undeploy']">
					<template #default="scope">
						<el-button v-auth="'flowable:modeler:undeploy'" type="primary" link @click="onUndeployModeler(scope.row)">撤销部署</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onPageSizeChange"
				@current-change="onPageCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 40, 50, 60]"
				v-model:current-page="search.pageNum"
				background
				v-model:page-size="search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getModelerDeployList, undeployModeler, getModelerDiagram } from '/@/api/flowable/modeler/index';
export default {
	name: 'flowable/modeler/deploy',
	components: { },
	setup() {
		const tableModelerDeployRef = ref();
		const state = reactive({
			search:{
				modelName: '',
				modelKey: '',
				category: '',
				pageNum: 1,
				pageSize: 10
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params: any) => {
			getModelerDeployList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = [];
					for(let i = 0; i < res.data.length; i++){
						loadModelerDiagram(res.data[i].modelKey).then(url =>{
							res.data[i].diagramUrl = url;
							state.tableData.data.push(res.data[i]);
						});
					}
					state.tableData.total = res.total;
					tableModelerDeployRef.value.clearSelection();
				}
			});
		}
		// 加载预览流程图
		const loadModelerDiagram = async (modelKey: string) => {
			const blob = await getModelerDiagram(modelKey);
			const url = window.URL.createObjectURL(blob);
			return url;
		}
		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.search.modelName = '';
			state.search.modelKey = '';
			state.search.category = '';
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections: any) => {
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		//部署模板
		const onUndeployModeler = (row: any) => {
			ElMessageBox.confirm('确定撤销部署【' + row.modelName + '】流程信息吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				undeployModeler({deployId: row.id}).then((res) => {
					if(res.code === 0){
						ElMessage.success('撤销部署成功');
						loadTableData({});
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData({});
		});
		return {
			tableModelerDeployRef,
			onTableSearch,
			onResetSearch,
			onTableSelectionChange,
			onPageSizeChange,
			onPageCurrentChange,
			onUndeployModeler,
			getModelerDiagram,
			...toRefs(state),
		};
	},
};
</script>